package q43_multiply;

public class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        StringBuilder a = new StringBuilder();
        a.append("a");
        a.append("b");
        System.out.println(a.reverse());

        System.out.println(solution.multiply("99", "999"));
        System.out.println(999 * 99);
        String b = "123";
        // System.out.println(addStrings("891","8910"));
    }

    private String multiply(String num1, String num2) {

        if (num1.equals("0") || num2.equals("0")){
            return "0";
        }

        StringBuilder bin = new StringBuilder();
        String res = "0";
        for (int i = num2.length() - 1; i >= 0 ; i--) {

            bin.setLength(0);
            int mod,carry = 0;
            bin.append("0".repeat(Math.max(0, num2.length() - 1 - i)));

            for (int j = num1.length() - 1; j >= 0 ; j--) {
                mod = ((num2.charAt(i) - '0') * (num1.charAt(j) - '0') + carry) % 10;
                carry = ((num2.charAt(i) - '0') * (num1.charAt(j) - '0') + carry) / 10;
                bin.append(mod);
            }

            if (carry != 0) {
                bin.append(carry);
            }

            // System.out.println(bin);

            res = addStrings(res,bin.reverse().toString());

        }


        return res;
    }


    public static String addStrings(String a, String b){
        if (a.equals("0") || b.equals("0")){
            return a.equals("0") ? b : a;
        }
        StringBuilder sb = new StringBuilder();
        int i = a.length() - 1, j = b.length() - 1, add = 0;

        while (i >= 0 || j >= 0 || add != 0){
            int v1 = i >= 0 ? a.charAt(i) - '0' : 0;
            int v2 = j >= 0 ? b.charAt(j) - '0' : 0;
            int res = v1 + v2 + add;
            sb.append(res % 10);
            add = res / 10;
            i--;
            j--;
        }


        return sb.reverse().toString();

    }

}
